package com.epfresh.supplier.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderOnGroupList;
import com.epfresh.supplier.entity.OrderOnGroupMinus;

import java.util.List;

/**
 * Created by zzq on 2016/7/19.
 */
public class OrderOnGroupMinusAdapter extends BaseAdapter implements View.OnClickListener {

    public ListView lv;
    public Context context;
    public List<OrderOnGroupMinus> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OrderOnGroupMinusAdapter(Context context, ListView lv, List<OrderOnGroupMinus> groupList) {
        this.context = context;
        this.list = groupList;
        this.lv = lv;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int groupPosition, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_order_on_group_minus, null, false);
            vh.tv_item_order_on_group_minus_base = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_base);

            vh.iv_item_order_on_group_minus_product = (ImageView) convertView.findViewById(R.id.iv_item_order_on_group_minus_product);
            vh.tv_item_order_on_group_minus_product_title = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_product_title);
            vh.tv_item_order_on_group_minus_product_price = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_product_price);
            vh.tv_item_order_on_group_minus_product_size_detail = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_product_size_detail);
            vh.tv_item_order_on_group_minus_money = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_money);

            vh.iv_item_order_on_group_minus_state = (ImageView) convertView.findViewById(R.id.iv_item_order_on_group_minus_state);
            vh.tv_item_order_on_group_minus_state = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_state);

            vh.tv_item_order_on_group_minus_detail = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_detail);
            vh.tv_item_order_on_group_minus_cancel = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_cancel);
            vh.tv_item_order_on_group_minus_again = (TextView) convertView.findViewById(R.id.tv_item_order_on_group_minus_again);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.tv_item_order_on_group_minus_again.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_minus_again.setOnClickListener(this);

        vh.tv_item_order_on_group_minus_cancel.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_minus_cancel.setOnClickListener(this);

        vh.tv_item_order_on_group_minus_detail.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_on_group_minus_detail.setOnClickListener(this);

        OrderOnGroupMinus order = list.get(groupPosition);
        vh.tv_item_order_on_group_minus_base.setText(order.getBaseStoreName());
        vh.tv_item_order_on_group_minus_product_title.setText(order.getProductTitle());
        vh.tv_item_order_on_group_minus_product_price.setText("单价："+order.getProductPriceUnit()+"元/"+order.getChargeUnit());
        vh.tv_item_order_on_group_minus_product_size_detail.setText("数量："+order.getProductCount()+order.getChargeUnit());
        ImageLoader.getInstance().displayImage(order.getThumbnail(), vh.iv_item_order_on_group_minus_product);
        vh.tv_item_order_on_group_minus_money.setText("退款金额："+order.getApplyPrice()+"元");


        initState(order,vh);
        initButton(order,vh);
        convertView.setTag(vh);
        convertView.setClickable(true);
        return convertView;
    }

    public class ViewHolder {

        TextView tv_item_order_on_group_minus_base;

        ImageView iv_item_order_on_group_minus_product;
        TextView tv_item_order_on_group_minus_product_title;
        TextView tv_item_order_on_group_minus_product_price;
        TextView tv_item_order_on_group_minus_product_size_detail;
        TextView tv_item_order_on_group_minus_money;

        TextView tv_item_order_on_group_minus_state;
        ImageView iv_item_order_on_group_minus_state;

        TextView tv_item_order_on_group_minus_cancel;
        TextView tv_item_order_on_group_minus_detail;
        TextView tv_item_order_on_group_minus_again;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
// 交货码，查看物流，取消订单，确认收货，支付， 详情
            onItemClickListener.onItemClick(null,v,position,position);
        }
    }

    void initState(OrderOnGroupMinus bean,ViewHolder vh){
        vh.tv_item_order_on_group_minus_state.setText(bean.getStatusTitle());
        if(bean.getRefundStatus()!=null){
            switch (bean.getRefundStatus()){
                case 10:
                    vh.iv_item_order_on_group_minus_state.setImageResource(R.mipmap.order_group_minus_return_waiting);
                    vh.tv_item_order_on_group_minus_state.setTextColor(Color.parseColor("#ff7900"));
                    break;
                case 20:
                    vh.iv_item_order_on_group_minus_state.setImageResource(R.mipmap.order_group_minus_success);
                    vh.tv_item_order_on_group_minus_state.setTextColor(Color.parseColor("#3eb049"));
                    break;
                case 30:
                    vh.iv_item_order_on_group_minus_state.setImageResource(R.mipmap.order_group_minus_fail);
                    vh.tv_item_order_on_group_minus_state.setTextColor(Color.parseColor("#ff0000"));
                    break;
            }
        }else{
            switch (bean.getJmtStatus()){
                case 10:
                    vh.iv_item_order_on_group_minus_state.setImageResource(R.mipmap.order_group_minus_look_waiting);
                    vh.tv_item_order_on_group_minus_state.setTextColor(Color.parseColor("#434343"));
                    break;
                case 20:
                    vh.iv_item_order_on_group_minus_state.setImageResource(R.mipmap.order_group_minus_done);
                    vh.tv_item_order_on_group_minus_state.setTextColor(Color.parseColor("#434343"));
                    break;
                case 30:
                    vh.iv_item_order_on_group_minus_state.setImageResource(R.mipmap.order_group_minus_sure);
                    vh.tv_item_order_on_group_minus_state.setTextColor(Color.parseColor("#2caeeb"));
                    break;
                case 40:
                    vh.iv_item_order_on_group_minus_state.setImageResource(R.mipmap.order_group_minus_refuse);
                    vh.tv_item_order_on_group_minus_state.setTextColor(Color.parseColor("#ff0000"));
                    break;
            }
        }
    }

    void initButton(OrderOnGroupMinus order,ViewHolder vh){
        vh.tv_item_order_on_group_minus_cancel.setVisibility(View.GONE);
        vh.tv_item_order_on_group_minus_again.setVisibility(View.GONE);
        vh.tv_item_order_on_group_minus_detail.setVisibility(View.GONE);

        for (OrderOnGroupList.ButtonListBean bean:order.getButtonList()) {
            switch (bean.getCode()){
                case "supplyViewJmt":
                    vh.tv_item_order_on_group_minus_detail.setVisibility(View.VISIBLE);
                    break;

                case "supplyApplyJmtAgain":
                    vh.tv_item_order_on_group_minus_again.setVisibility(View.VISIBLE);
                    break;
                case "supplyCancelJmt":
                    vh.tv_item_order_on_group_minus_cancel.setVisibility(View.VISIBLE);
                    break;
            }

        }


    }
}
